package IOStream.Exercise001;

import java.io.*;

/**
 * ClassName:Exercise005
 * Package:IOStream.Exercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/2-20:33
 * @Version:v1.0
 */
public class Exercise005 {
    /**
     * 四种方式拷贝文件,并统计各自用时
     * 1.字节流的基本流：一次读写一个字节
     * 2.字节流的基本流：一次读写一个字节数组
     * 3.字节缓冲流：一次读写一个字节
     * 4.字节缓冲流：一次读写一个字节数组
     */
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
//        method1();//0.022秒
//        method2();//0.001秒
//        method3();//0.003秒
//        method4();//0.0秒
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000.0 + "秒");
    }

    private static void method4() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\aaa\\csb.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\aaa\\csb4.txt"));
        int b;
        byte[] bytes = new byte[8192];
        while ((b = bis.read(bytes)) != -1) {
            bos.write(bytes,0,b);
        }
        bos.close();
        bis.close();
    }

    //3.字节缓冲流：一次读写一个字节
    private static void method3() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\aaa\\csb.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\aaa\\csb3.txt"));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        bos.close();
        bis.close();
    }

    //字节流的基本流：一次读写一个字节数组
    private static void method2() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\aaa\\csb.txt");
        FileOutputStream fos=new FileOutputStream("E:\\aaa\\csb2.txt");
        int b;
        byte[] bytes = new byte[8192];
        while ((b = fis.read(bytes)) != -1) {
            fos.write(bytes,0,b);
        }
        fos.close();
        fis.close();
    }

    //字节流的基本流：一次读写一个字节
    private static void method1() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\aaa\\csb.txt");
        FileOutputStream fos = new FileOutputStream("E:\\aaa\\csb1.txt");
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fos.close();
        fis.close();
    }
}
